package com.gs.synedocket.util;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.gs.synedocket.R;
import com.gs.synedocket.constant.AllConstants;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    public static String DateTimeToStr(Date date) {
        return DateTimeToStr(date, "yyyy-MM-dd");
    }

    public static String DateTime2Str(Date date) {
        return DateTimeToStr(date, "yyyy-MM-dd HH:mm");
    }

    public static String DateTimeToStr(Date date, String format) {
        //可根据需要自行截取数据显示
        if (date == null)
            return "";
        SimpleDateFormat simpleDateFormat = null;
        try {
            simpleDateFormat = new SimpleDateFormat(format, SPUtil.getInstance().getInt(AllConstants.LANGUAGE, 1) == 1 ? Locale.TAIWAN : Locale.ENGLISH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return simpleDateFormat.format(date);
    }

    public static Date StrToDateTime(String str) {
        return StrToDateTime(str, "yyyy-MM-dd");
    }
    public static Date Str2DateTime(String str) {
        return StrToDateTime(str, "yyyy-MM-dd HH:mm");
    }

    public static Date StrToDateTime(String str, String format) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format,SPUtil.getInstance().getInt(AllConstants.LANGUAGE, 1) == 1 ? Locale.TAIWAN : Locale.ENGLISH);
            return simpleDateFormat.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public interface ShowTimeSelectorCallback {
        void OnTimeSelectListener(Date date);
    }

    public static void showDateSelector(Context context, int title, View v, Calendar selectCalendar, final ShowTimeSelectorCallback callback) {
        showDateSelector(context, title, v, selectCalendar, "yyyyMMdd", callback);
    }

    public static void showDateSelector(Context context, int title, View v, Calendar selectCalendar, String format, final ShowTimeSelectorCallback callback) {
        WindowUtil.getInstance().hideSoftInput(v);
        TimePickerBuilder builder = new TimePickerBuilder(context, (date, v1) -> {
            if (callback != null) {
                callback.OnTimeSelectListener(date);
            }
        });
        Calendar startDate = Calendar.getInstance();
        startDate.set(2000, 1, 01);
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.set(calendarEnd.get(Calendar.YEAR) + 10, 12, 31);
        TimePickerView timePickerView = builder
                .setType(new boolean[]{format.contains("yyyy"),
                        format.contains("MM"),
                        format.contains("dd"),
                        format.contains("HH"),
                        format.contains("mm"),
                        format.contains("ss")})
                .setLabel(
                        format.contains("yyyy") ? context.getString(R.string.year) : "",
                        format.contains("MM") ? context.getString(R.string.month) : "",
                        format.contains("dd") ? context.getString(R.string.day) : "",
                        format.contains("HH") ? context.getString(R.string.hours) : "",
                        format.contains("mm") ? context.getString(R.string.min) : "",
                        format.contains("ss") ? context.getString(R.string.seconds) : "")
                .isCenterLabel(true).setDividerColor(Color.DKGRAY).setCancelText(context.getString(R.string.cancel))
                .setSubmitText(context.getString(R.string.confirm))
                .setTitleText(context.getString(title))
                .setLineSpacingMultiplier(3.0f)
                .setItemVisibleCount(7)
                .setDecorView(null)
                .setRangDate(startDate, calendarEnd)
                .build();
        timePickerView.setDate(selectCalendar);
        timePickerView.show();
    }

    /**
     * 获取当前日期往前num天
     * @param date
     * @param num
     * @return
     */
    public static String getFrontDate(Date date, int num){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int oldDay = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH,oldDay-num+1);
        return DateTimeToStr(calendar.getTime());
    }

    /**
     * 获取当月第一天
     * @return
     */
    public static String getBeginDayOfMonth(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,1);
        return DateTimeToStr(calendar.getTime())+" 00:00:00";
    }

    /**
     * 获取当月最后一天
     * @return
     */
    public static String getEndDayOfMonth(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DateTimeToStr(calendar.getTime())+" 23:59:59";
    }
}
